package spring.cloud.tasks.tasks_assign_manager.service;

import com.google.common.base.Strings;
import lombok.extern.slf4j.Slf4j;
import org.apache.curator.framework.CuratorFramework;
import spring.cloud.tasks.common.node.TaskNodePath;
import spring.cloud.tasks.common.zookeeper.ZooKeeper;
import spring.cloud.tasks.common.zookeeper.ZooKeeperRepository;
import spring.cloud.tasks.tasks_assign_manager.BootStrap;
import spring.cloud.tasks.tasks_assign_manager.database.service.NameSpaceDtoService;
import spring.cloud.tasks.tasks_assign_manager.database.service.NameSpaceZooKeeperClusterMappingDtoService;
import spring.cloud.tasks.tasks_assign_manager.database.service.TaskConfigurationDtoService;
import spring.cloud.tasks.tasks_assign_manager.database.service.ZooKeeperClusterDtoService;
import spring.cloud.tasks.tasks_assign_manager.domain.NameSpace;
import spring.cloud.tasks.tasks_assign_manager.domain.NameSpaceZooKeeperClient;
import spring.cloud.tasks.tasks_assign_manager.domain.ZooKeeperCluster;
import spring.cloud.tasks.tasks_assign_manager.domain.ZooKeeperTaskConfiguration;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

@Slf4j
public class TaskService {


    @Resource
    private ZooKeeperClusterDtoService zooKeeperClusterDtoService;
    @Resource
    private NameSpaceDtoService nameSpaceDtoService;
    @Resource
    private NameSpaceZooKeeperClusterMappingDtoService nameSpaceZooKeeperClusterMappingDtoService;
    @Resource
    private TaskConfigurationDtoService taskConfigurationDtoService;
    @Resource
    private ZooKeeperRepository zooKeeperRepository;

    public NameSpace getNameSpace(String nameSpace) {
        if (Strings.isNullOrEmpty(nameSpace)) {
            return null;
        }
        Collection<ZooKeeperCluster> zooKeeperClusterCollection = BootStrap.zooKeeperClusterKeyToZooKeeperClusterMap.values();
        for (ZooKeeperCluster zooKeeperCluster : zooKeeperClusterCollection) {
            for (NameSpace $nameSpace : zooKeeperCluster.getNameSpaceList()) {
                if ($nameSpace != null && nameSpace.equals($nameSpace.getNameSpace())) {
                    return $nameSpace;
                }
            }
        }
        return null;
    }


    public ZooKeeper getZooKeeper(String nameSpace) throws RuntimeException {
        ZooKeeper zooKeeper = null;
        try {
            NameSpace nameSpace1 = getNameSpace(nameSpace);
            if (nameSpace1 == null) {
                throw new RuntimeException("find registryCenterConfiguration failed");
            }
            String zooKeeperAddressList = nameSpace1.getZooKeeperAddressList();
            String digest = nameSpace1.getDigest();
            synchronized (BootStrap.getNameSpaceLock(nameSpace)) {
                if (!BootStrap.nameSpaceTonameSpaceZooKeeperClientMap.containsKey(nameSpace)) {
                    final NameSpaceZooKeeperClient nameSpaceZooKeeperClient = new NameSpaceZooKeeperClient();
                    nameSpaceZooKeeperClient.setNameSpace(nameSpace);
                    nameSpaceZooKeeperClient.setZooKeeperAddress(zooKeeperAddressList);
                    CuratorFramework curatorFramework = zooKeeperRepository.connect(zooKeeperAddressList, digest, nameSpace);
                    if (curatorFramework != null) {
                        nameSpaceZooKeeperClient.setConnected(curatorFramework.getZookeeperClient().isConnected());
                        nameSpaceZooKeeperClient.setCuratorFramework(curatorFramework);
                        BootStrap.nameSpaceTonameSpaceZooKeeperClientMap.put(nameSpace, nameSpaceZooKeeperClient);
                        zooKeeper = zooKeeperRepository.getZooKeeper(curatorFramework);
                    }
                } else {
                    NameSpaceZooKeeperClient nameSpaceZooKeeperClient = BootStrap.nameSpaceTonameSpaceZooKeeperClientMap.get(nameSpace);
                    if (nameSpaceZooKeeperClient != null) {
                        CuratorFramework curatorFramework = nameSpaceZooKeeperClient.getCuratorFramework();
                        if (curatorFramework != null) {
                            nameSpaceZooKeeperClient.setConnected(curatorFramework.getZookeeperClient().isConnected());
                            zooKeeper = zooKeeperRepository.getZooKeeper(curatorFramework);
                        }
                    }
                }
            }
        } catch (RuntimeException e) {
            throw e;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        if (zooKeeper == null) {
            throw new RuntimeException("Connect zookeeper failed");
        }
        return zooKeeper;
    }


    public List<String> getTaskIdList(String nameSpace) throws RuntimeException {
        List<String> taskIdList = taskConfigurationDtoService.getTaskIdListBynameSpace(nameSpace);
        return taskIdList != null ? taskIdList : new ArrayList<>();
    }

    /**
     * 返回的taskId有的可能是正常状态的 有的可能是非正常状态的
     *
     * @param nameSpace
     * @return
     */
    public List<ZooKeeperTaskConfiguration> getTaskConfigurationListFromZooKeeper(String nameSpace) {
        ZooKeeper zooKeeper = getZooKeeper(nameSpace);
        List<String> taskIdList = zooKeeper.getChildren(TaskNodePath.tasks);
        List<ZooKeeperTaskConfiguration> zooKeeperTaskConfigurationList = new ArrayList<ZooKeeperTaskConfiguration>();
        for (String taskId : taskIdList) {
            //
        }
        return zooKeeperTaskConfigurationList;
    }


}
